package com.intelligence.pda.ui.login;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.intelligence.androidlibrary.utils.SharePreferenceUtils;
import com.intelligence.pda.R;
import com.intelligence.pda.parameter.LoginParameter;
import com.intelligence.pda.parameter.NewBasicStrParameter;
import com.intelligence.pda.retrofit.ApiCallback;
import com.intelligence.pda.ui.BaseActivity;
import com.intelligence.pda.ui.login.model.LoginModel;
import com.intelligence.pda.ui.login.model.UserInfoModel;
import com.intelligence.pda.ui.main.MainActivity;
import com.intelligence.pda.utils.AppConstants;

import butterknife.Bind;

public class LoginActivity extends BaseActivity implements View.OnClickListener{
    @Bind(R.id.et_name)
    EditText et_name;
    @Bind(R.id.et_pwd)
    EditText et_pwd;
    @Bind(R.id.tv_login)
    TextView tv_login;
    @Bind(R.id.tv_pwd_no)
    TextView tv_pwd_no;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        if (!TextUtils.isEmpty(SharePreferenceUtils.getString(AppConstants.EDIT_NAME))
                && !TextUtils.isEmpty(SharePreferenceUtils.getString(AppConstants.EDIT_PWD))) {
            et_name.setText(SharePreferenceUtils.getString(AppConstants.EDIT_NAME));
            et_pwd.setText(SharePreferenceUtils.getString(AppConstants.EDIT_PWD));
            btnLogin();
        }

        Listener();
    }

    private void Listener() {
        tv_login.setOnClickListener(this);
        tv_pwd_no.setOnClickListener(this);
    }


    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_login:
                //登录
                if (getPwd()) {
                    btnLogin();
                }
                break;
            case R.id.tv_pwd_no:
                //忘记密码
                break;

        }
    }

    private boolean getPwd() {
        if (TextUtils.isEmpty(et_name.getText().toString())) {
            toastShow("用户名不能为空");
            return false;
        }
        if (TextUtils.isEmpty(et_pwd.getText().toString())) {
            toastShow("密码不能为空");
            return false;
        }
        if (et_name.getText().toString().length() < 2) {
            toastShow("用户名需要4~16位之间");
            return false;
        }
        if (et_pwd.getText().toString().length() < 6) {
            toastShow("密码需要6~16位之间");
            return false;
        }
        return true;
    }

    private void btnLogin() {
        LoginParameter loginParameter = new LoginParameter();
        loginParameter.setUserName(et_name.getText().toString());
        loginParameter.setUserPswd(et_pwd.getText().toString());
        showProgressDialog();
        addSubscription(apiStores.userLogin(loginParameter),
                new ApiCallback<LoginModel>() {
                    @Override
                    public void onSuccess(LoginModel model) {
                        SharePreferenceUtils.putString(AppConstants.BASIC_STR, model.getData().getBasicStr());
                        getUserInfo();
                    }

                    @Override
                    public void onFailure(LoginModel model, String msg) {
                        toastShow(msg);
                    }

                    @Override
                    public void onFailure(String msg) {
                        toastShow(msg);
                    }


                    @Override
                    public void onFinish() {
                        dismissProgressDialog();
                    }

                });
    }

    private void getUserInfo() {
        //获取用户信息
        NewBasicStrParameter basicStrParameter = new NewBasicStrParameter();
        basicStrParameter.setBasicStr(SharePreferenceUtils.getString(AppConstants.BASIC_STR));
        addSubscription(apiStores.getUserInfo(basicStrParameter),
                new ApiCallback<UserInfoModel>() {
                    @Override
                    public void onSuccess(UserInfoModel model) {
                        getUserInfoSuccess(model);
                    }

                    @Override
                    public void onFailure(UserInfoModel model, String msg) {
                        toastShow(msg);
                    }

                    @Override
                    public void onFailure(String msg) {
                        toastShow(msg);
                    }


                    @Override
                    public void onFinish() {
                    }

                });
    }

    private void getUserInfoSuccess(UserInfoModel userInfoModel) {
        SharePreferenceUtils.putString(AppConstants.PERSON_NAME, userInfoModel.getData().getPersonName());
        SharePreferenceUtils.putString(AppConstants.USER_NAME, userInfoModel.getData().getUserName());
        SharePreferenceUtils.putString(AppConstants.MOBILE, userInfoModel.getData().getMobile());
        SharePreferenceUtils.putString(AppConstants.EDIT_NAME, et_name.getText().toString());
        SharePreferenceUtils.putString(AppConstants.EDIT_PWD, et_pwd.getText().toString());
        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
        startActivity(intent);
        finish();
    }
}
